home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ASME's Mechanical Engine…ing Toolkit 1997 December
/
ASME's Mechanical Engineering Toolkit 1997 December.iso
/
c_lang
/
varinc.lzh
/
PAGE292.C
< prev
next >
Wrap
Text File
|
1979-11-30
|
1KB
|
35 lines
/* Compare array indexing using [] and using pointer access. */
#include <time.h>
#define LDIM 5000
main(ac, av)
unsigned ac;
char *av[];
{
long longs[LDIM], sum; /* Use /STACK:32768 LINK flag. */
long beg_time, mid_time, end_time;
long *p_long = longs, *p_long2 = longs;
short ilong, itrial, ntrials = ((ac > 1) ? atoi(av[1]) : 1);
time(&beg_time); /* array index timing */
for (itrial = 0; itrial < ntrials; ++itrial)
{
for (ilong = 0; ilong < LDIM; ++ilong)
longs[ilong] = ilong;
for (sum = ilong = 0; ilong < LDIM; ++ilong)
sum += longs[ilong];
}
time(&mid_time); /* pointer-arithmetic timing */
for (itrial = 0; itrial < ntrials; ++itrial)
{
for (ilong = 0; ilong < LDIM; ++ilong)
*(p_long + ilong) = ilong;
for (sum = ilong = 0; ilong < LDIM; ++ilong)
sum += *(p_long2 + ilong);
}
time(&end_time);
printf("Array-Subscript Time: %4ld\n", mid_time - beg_time);
printf("Pointer-Arithmetic Time: %4ld\n", end_time - mid_time);
}